package com.eurasia.assignment.controller.dto;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import com.eurasia.assignment.domain.Submission;
import com.eurasia.assignment.domain.SubmissionStatus;
import com.eurasia.assignment.infrastructure.entity.FileEntity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SubmissionResponse {
    private String id;
    private Long projectId;
    private String description;
    private String submitterId;
    @Builder.Default
    private List<FileDTO> files = new ArrayList<>();
    private LocalDateTime submissionTime;
    private String reviewResult;
    private SubmissionStatus reviewStatus;
    private String type;
    private Long reviewerId;
    private LocalDateTime reviewTime;
    
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class FileDTO {
        private Long id;
        private String originalFileName;
        private String storedFileName;
        private String filePath;
        private Long fileSize;
        private String fileType;
        private LocalDateTime uploadDate;
        
        public static FileDTO fromEntity(FileEntity entity) {
            if (entity == null) {
                return null;
            }
            
            return FileDTO.builder()
                    .id(entity.getId())
                    .originalFileName(entity.getOriginalFileName())
                    .storedFileName(entity.getStoredFileName())
                    .filePath(entity.getFilePath())
                    .fileSize(entity.getFileSize())
                    .fileType(entity.getFileType())
                    .uploadDate(entity.getUploadDate())
                    .build();
        }
    }
    
    public static SubmissionResponse fromDomain(Submission submission) {

        return SubmissionResponse.builder()
                .id(submission.getId())
                .projectId(submission.getProjectId())
                .description(submission.getDescription())
                .submitterId(submission.getSubmitterId())
                .files(List.of(FileDTO.fromEntity(submission.getFile())))
                .submissionTime(submission.getSubmissionTime())
                .reviewResult(submission.getReviewResult())
                .reviewStatus(submission.getReviewStatus())
                .type(submission.getType())
                .reviewerId(submission.getReviewerId())
                .reviewTime(submission.getReviewTime())
                .build();
    }
} 